package com.sanbi.erp.core.util;

import java.util.Locale;
import java.util.Random;

public class CharUtil {

    public static String getRandomString(Integer num) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        return getString(num, base);
    }

    /**
     * 随机生成 num 位数字
     *
     * @param num
     * @return
     */
    public static String getRandomNum(Integer num) {
        String base = "0123456789";
        return getString(num, base);
    }

    /**
     * 随机生成 num 位数字 + 大小写字母
     *
     * @param num
     * @return
     */
    public static String getRandomNumString(Integer num) {
//        String base = "Bdef0123ghTUVWXijkJKLMN789OPQlmnoIRSYZabwxyzACpqrstuvDEF456GH";
        String base = "1234567890";
        return getString(num, base);
    }

    private static String getString(Integer num, String base) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < num; i++) {
            //生成num位ascii码对应的数字
            int number = random.nextInt(base.length());
            //转成对应ASCII码对应字符
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}
